let getUserInfo = () => {
  return new Promise((resolve, reject) => {
    uni.getUserInfo({
      //成功后会返回
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};

const req = async () => {
  await getUserInfo();
  return new Promise((resolve) => {
    let obj = {};
    uni.login({
      //成功放回
      success: (res) => {
        let code = res.code;
        let appid = "wxd13ec5e936e7d88d";
        let secret = "4b0551f4daf11bf9b1f1d35610f2833f";
        let grant_type = "authorization_code";
        // 通过code换取openId
        uni.request({
          url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=${grant_type}`,
          success: (resOpenId) => {
            Object.assign(obj, resOpenId.data);
            uni.request({
              url: "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd13ec5e936e7d88d&secret=4b0551f4daf11bf9b1f1d35610f2833f",
              success: (accessToken) => {
                Object.assign(obj, accessToken.data);
                resolve(obj);
              },
            });
          },
        });
      },
    });
  });
};

export default req;
